home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / misc / samples2 / filefind.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-07-24  |  6.7 KB  |  202 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "File Find"
  4.    ClientHeight    =   5820
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1770
  7.    ClientWidth     =   7365
  8.    Height          =   6510
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5820
  12.    ScaleWidth      =   7365
  13.    Top             =   1140
  14.    Width           =   7485
  15.    Begin CommandButton Command1 
  16.       Caption         =   "&Search"
  17.       Default         =   -1  'True
  18.       Height          =   480
  19.       Left            =   5430
  20.       TabIndex        =   4
  21.       Top             =   765
  22.       Width           =   1575
  23.    End
  24.    Begin TextBox Text1 
  25.       Height          =   330
  26.       Left            =   165
  27.       TabIndex        =   2
  28.       Text            =   "Text1"
  29.       Top             =   915
  30.       Width           =   2280
  31.    End
  32.    Begin ListBox List1 
  33.       Height          =   3735
  34.       Left            =   180
  35.       TabIndex        =   1
  36.       Top             =   1485
  37.       Width           =   6885
  38.    End
  39.    Begin ListBox lstFastFiles 
  40.       Height          =   420
  41.       Left            =   210
  42.       TabIndex        =   0
  43.       Top             =   210
  44.       Visible         =   0   'False
  45.       Width           =   1215
  46.    End
  47.    Begin Label Label2 
  48.       Caption         =   "Label2"
  49.       Height          =   345
  50.       Left            =   195
  51.       TabIndex        =   5
  52.       Top             =   5400
  53.       Width           =   2595
  54.    End
  55.    Begin Label Label1 
  56.       Caption         =   "Label1"
  57.       Height          =   870
  58.       Left            =   180
  59.       TabIndex        =   3
  60.       Top             =   30
  61.       Width           =   6915
  62.    End
  63.    Begin Menu mnuExit 
  64.       Caption         =   "Exit"
  65.    End
  66. 'Used in the hard drive search routines
  67. Const CHUNK = 10  ' Used for allocation of array space - how many elements at a time ?
  68. Const FILECHUNK = 10
  69. ' Constants for API calls
  70. Const WM_USER = &H400
  71. Const LB_DIR = WM_USER + 14
  72. Const SRCCOPY = &HCC0020
  73. ' Directory constants
  74. Const ATTR_ARCHIVE = 32
  75. Const ATTR_DIRECTORY = 16
  76. Const ATTR_VOLUME = 8
  77. Const ATTR_SYSTEM = 4
  78. Const ATTR_HIDDEN = 2
  79. Const ATTR_READONLY = 1
  80. Const ATTR_NORMAL = 0
  81. Dim Files() As FileInfo   ' Store the file info
  82. Dim FileCount As Integer   ' How many files are in the array
  83. Declare Function SendMessage Lib "user" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wp As Integer, lp As Any) As Long
  84. Sub AddFile (Path$, Filename$)
  85. 'Add a file to the structure
  86.     'Allocate more space if necessary
  87.     If (FileCount Mod FILECHUNK) = 0 Then
  88.         ReDim Preserve Files(FileCount + FILECHUNK)
  89.     End If
  90.     FileCount = FileCount + 1
  91.     Files(FileCount).Path = Path$
  92.     Files(FileCount).File = Filename$
  93. End Sub
  94. Sub Command1_Click ()
  95.     SearchDrives
  96. End Sub
  97. Function FilesFound () As Integer
  98. 'Informs the caller how many files are in the structure
  99.     FilesFound = FileCount
  100. End Function
  101. Sub Form_Load ()
  102.     Me.Show
  103.     msg$ = "Type in the file specification you want to search for.  "
  104.     msg$ = msg$ + "Wildcards are permitted.  For example, to find all .VBX files, "
  105.     msg$ = msg$ + "type ""*.vbx"".  NOTE:  You may get an out of memory error "
  106.     msg$ = msg$ + "(or worse) if your search locates a large number of files "
  107.     msg$ = msg$ + "(1200+)."
  108.     label1 = msg$
  109.     'Set default filespec
  110.     Text1 = "*.vbx"
  111.     Text1.SelStart = 0
  112.     Text1.SelLength = Len(Text1)
  113.     Text1.SetFocus
  114.     label2 = ""
  115. End Sub
  116. Sub Form_Unload (Cancel As Integer)
  117.     End
  118. End Sub
  119. Sub ListFiles (Path$, Ext$)
  120. ' List all the files in a directory
  121. Dim I As Integer, FileSpec As String
  122.     FileSpec = Path$ + "\" + Ext$
  123.     ' Tell Windows to fill the list box with the required file names
  124.     ' The 7 represents  ATTR_SYSTEM + ATTR_HIDDEN + ATTR_READONLY + ATTR_NORMAL
  125.     I = SendMessage(lstFastFiles.hWnd, LB_DIR, 7, ByVal FileSpec)
  126.     For I = 0 To lstFastFiles.ListCount - 1
  127.         Call AddFile(Path$, UCase$(lstFastFiles.List(I)))
  128.     Next I
  129.     lstFastFiles.Clear
  130. End Sub
  131. Sub ListSubDirs (Path$)
  132. Dim Count, Directories() As String, I, DirName As String  ' Declare variables.
  133. On Error GoTo errListSubDirs
  134.     DoEvents
  135.     'This is the filespec that will be searched for on all hard drives
  136.     FileSpec$ = Text1
  137.     Call ListFiles(Path$, FileSpec$)
  138.     DirName = Dir(Path$ & "\", ATTR_DIRECTORY + ATTR_HIDDEN)' Get first directory name.
  139.     'Iterate through PATH, caching all subdirectories in Directories()
  140.     Do While (DirName <> "") And (Not ErrorOccured)
  141.         If DirName <> "." And DirName <> ".." Then
  142.             If (GetAttr(Path$ & "\" & DirName) And ATTR_DIRECTORY) = ATTR_DIRECTORY Then
  143.                 If (Count Mod CHUNK) = 0 Then
  144.                     ReDim Preserve Directories(Count + CHUNK)    ' Resize the array.
  145.                 End If
  146.                 Count = Count + 1   ' Increment counter.
  147.                 Directories(Count) = DirName
  148.             End If
  149.         End If
  150.         DirName = Dir$   ' Get another directory name.
  151.     Loop
  152.     ' Now recursively iterate through each cached subdirectory.
  153.     I = 1
  154.     While (I <= Count) And (Not ErrorOccured)
  155.         Call ListSubDirs(Path$ & "\" & Directories(I))
  156.         I = I + 1
  157.     Wend
  158.     Exit Sub
  159. errListSubDirs:
  160.     MsgBox "Error reading subdirectories", 48
  161.     ErrorOccured = True
  162.     Exit Sub
  163. End Sub
  164. Sub mnuExit_Click ()
  165.     Unload Me
  166. End Sub
  167. Sub SearchDir ()
  168. 'Start the search
  169. Dim a As String, I As Integer
  170.     'Change to the root directory
  171.     ChDir "\"
  172.     a = CurDir$
  173.     'Remove any backslash
  174.     If Right$(a, 1) = "\" Then a = Left$(a, Len(a) - 1)
  175.     Call ListSubDirs(a)  ' Start the recursive traverse of the tree
  176. End Sub
  177. Sub SearchDrives ()
  178. On Error GoTo DriveError
  179.     Screen.MousePointer = 11
  180.     'Loop for every valid drive letter (C to Z)
  181.     For I = 67 To 90
  182.         label2 = "Searching drive " + Chr$(I) + ":"
  183.         'When you try to change to a drive that doesn't exist, an error
  184.         'occurs and the program jumps down to the DriveError label.
  185.         ChDrive Chr$(I)
  186.         SearchDir
  187.     Next I
  188. DriveError:
  189.         
  190.     label2 = ""
  191.     Screen.MousePointer = 0   ' Reset the mouse pointer
  192.     'This loop is where you would be likely to get an Out of Memory error if
  193.     'your search found a large number of files.  I'm sure there is probably
  194.     'a way to avoid it, but I didn't feel like messing with it.  After all,
  195.     'this is only a sample.  :)
  196.     For I = 1 To FileCount
  197.         List1.AddItem UCase$(Files(I).Path) & "\" & UCase$(Files(I).File)
  198.     Next I
  199.     label2 = "Files Found:  " & Str$(FileCount)
  200.     Exit Sub
  201. End Sub
  202.